<!DOCTYPE html>
<meta charset="utf-8">
<title>click() should do nothing on disabled elements</title>
<link rel="author" title="Domenic Denicola" href="mailto:d@domenic.me">
<link rel="help" href="https://html.spec.whatwg.org/multipage/#attr-button-type-submit-state">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<button>Click me</button>

<script>
"use strict";

async_test(t => {

  const element = document.querySelector("button");
  element.addEventListener("click", t.step_func_done(e => {
    assert_equals(e.bubbles, true);
    assert_equals(e.cancelable, true);
    assert_equals(e.isTrusted, false);
    // TODO composed
  }));

  element.click();

}, "clicking using click() should produce an event object with the correct basic Event values");

async_test(t => {

  const element = document.querySelector("button");
  element.addEventListener("click", t.step_func_done(e => {
    assert_equals(e.view, window);
    assert_equals(e.detail, 0);
    assert_equals(e.ctrlKey, false);
    assert_equals(e.shiftKey, false);
    assert_equals(e.altKey, false);
    assert_equals(e.metaKey, false);
  }));

  element.click();

}, "clicking using click() should produce an event object with the correct UIEventInit values");

async_test(t => {

  const element = document.querySelector("button");
  element.addEventListener("click", t.step_func_done(e => {
    assert_equals(e.ctrlKey, false);
    assert_equals(e.shiftKey, false);
    assert_equals(e.altKey, false);
    assert_equals(e.metaKey, false);

    assert_equals(e.getModifierState("AltGraph"), false);
    assert_equals(e.getModifierState("CapsLock"), false);
    assert_equals(e.getModifierState("Fn"), false);
    assert_equals(e.getModifierState("FnLock"), false);
    assert_equals(e.getModifierState("Hyper"), false);
    assert_equals(e.getModifierState("NumLock"), false);
    assert_equals(e.getModifierState("ScrollLock"), false);
    assert_equals(e.getModifierState("Super"), false);
    assert_equals(e.getModifierState("Symbol"), false);
    assert_equals(e.getModifierState("SymbolLock"), false);
  }));

  element.click();

}, "clicking using click() should produce an event object with the correct EventModifierInit values");

async_test(t => {

  const element = document.querySelector("button");
  element.addEventListener("click", t.step_func_done(e => {
    assert_equals(e.screenX, 0);
    assert_equals(e.screenY, 0);
    assert_equals(e.clientX, 0);
    assert_equals(e.clientY, 0);
    assert_equals(e.x, 0);
    assert_equals(e.y, 0);
    assert_equals(e.pageX, 0);
    assert_equals(e.pageY, 0);
    assert_equals(e.offsetX, 0);
    assert_equals(e.offsetY, 0);

    assert_equals(e.button, 0);
    assert_equals(e.buttons, 0);
    assert_equals(e.relatedTarget, null);
  }));

  element.click();

}, "clicking using click() should produce an event object with the correct MouseEventInit values");

test(() => {
  const e = new MouseEvent("click", {
    clientX: 1, clientY: 2, view: window
  });
  assert_equals(e.x, 1);
  assert_equals(e.y, 2);
  assert_equals(e.pageX, 1);
  assert_equals(e.pageY, 2);
  assert_equals(e.offsetX, 1);
  assert_equals(e.offsetY, 2);
}, "MouseEvent should provide the correct computed values when the dispatch flag is not set");

async_test(t => {
  const element = document.documentElement; // dispatch against something at the origin (0,0) so it passes in browsers
  element.addEventListener("click", t.step_func_done(e => {
    assert_equals(e.pageX, 1);
    assert_equals(e.pageY, 2);
    assert_equals(e.offsetX, 1);
    assert_equals(e.offsetY, 2);
  }));
  element.dispatchEvent(new MouseEvent("click", {
    clientX: 1, clientY: 2, view: window
  }));
}, "MouseEvent should provide the correct computed values when the dispatch flag is set");

test(() => {
  const e = new MouseEvent("click", {
    screenX: 1.5, screenY: 2.5, clientX: 3.5, clientY: 4.5
  });
  assert_equals(e.screenX, 1.5);
  assert_equals(e.screenY, 2.5);
  assert_equals(e.clientX, 3.5);
  assert_equals(e.clientY, 4.5);
}, "MouseEvent.constructor should preserve doubles");

test(() => {
  const e = new MouseEvent("click");
  e.initMouseEvent("click", true, true, null, null, 1.5, 2.5, 3.5, 4.5);
  assert_equals(e.screenX, 1);
  assert_equals(e.screenY, 2);
  assert_equals(e.clientX, 3);
  assert_equals(e.clientY, 4);
}, "MouseEvent.initMouseEvent should convert doubles to longs");
</script>
